package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.VideoItemDao;
import com.onlinevideo.entity.VideoItem;

import java.util.List;

public class VideoItemDaoImpl extends BaseDao implements VideoItemDao {
    @Override
    public int insert(VideoItem videoItem) {
        String sql ="INSERT INTO `T_video_item` (`id`,`video_id`,`url`,`sort_count`,`name`) VALUES (?,?,?,?,?)";
        return super.update(sql,videoItem.getId(),videoItem.getVideoId(),videoItem.getUrl(),videoItem.getSortCount(),videoItem.getName());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_video_item` WHERE `id`=?";
        return super.update(sql,id);
    }

    @Override
    public int deleteByVideoId(Long id) {
        String sql = "DELETE FROM `T_video_item` WHERE `video_id`=?";
        return super.update(sql,id);
    }

    @Override
    public int update(VideoItem videoItem) {
        String sql = "UPDATE `T_video_item` SET `video_id`=?,`url`=?,`sort_count`=?,`name`=? WHERE `id`=?";
        return super.update(sql,videoItem.getVideoId(),videoItem.getUrl(),videoItem.getSortCount(),videoItem.getName(),videoItem.getId());
    }

    @Override
    public int update(String name,Integer sortCount,Long id) {
        String sql = "UPDATE `T_video_item` SET `sort_count`=?,`name`=? WHERE `id`=?";
        return super.update(sql,sortCount,name,id);
    }

    @Override
    public VideoItem queryById(Long id) {
        String sql ="SELECT `id`,`video_id` AS `videoId`,`url`,`sort_count` AS `sortCount`,`name`  FROM `T_video_item` WHERE 1=1 AND `id`=? ";
        return super.queryForOne(VideoItem.class,sql,id);
    }

    @Override
    public List<VideoItem> queryAll() {
        String sql ="SELECT `id`,`video_id` AS `videoId`,`url`,`sort_count` AS `sortCount`,`name` FROM `T_video_item` WHERE 1=1 ";
        return super.queryForList(VideoItem.class,sql);
    }

    @Override
    public List<VideoItem> queryByVideo(Long id) {
        String sql ="SELECT `id`,`video_id` AS `videoId`,`url`,`sort_count` AS `sortCount`,`name` FROM `T_video_item` WHERE `video_id`=? ORDER BY `sort_count`";
        return super.queryForList(VideoItem.class,sql,id);
    }
}
